package com.coderman.domainservice.blogtopic.infrast.gatawayimpl;

import com.coderman.domainservice.blogtopic.domain.convertor.BlogCommentConvertor;
import com.coderman.domainservice.blogtopic.domain.convertor.BlogConvertor;
import com.coderman.domainservice.blogtopic.domain.gataway.BlogRepository;
import com.coderman.domainservice.blogtopic.domain.model.entity.BlogAggregateBO;
import com.coderman.domainservice.blogtopic.domain.model.entity.BlogBO;
import com.coderman.domainservice.blogtopic.domain.model.entity.BlogCommentBO;
import com.coderman.domainservice.blogtopic.infrast.mapper.dao.BlogCommentMapper;
import com.coderman.domainservice.blogtopic.infrast.mapper.dao.BlogContentMapper;
import com.coderman.domainservice.blogtopic.infrast.mapper.dao.BlogMapper;
import com.coderman.domainservice.blogtopic.infrast.mapper.dao.BlogSpecialColumnRelationMapper;
import com.coderman.domainservice.blogtopic.infrast.mapper.dataobject.BlogCommentDO;
import com.coderman.domainservice.blogtopic.infrast.mapper.dataobject.BlogContentDO;
import com.coderman.domainservice.blogtopic.infrast.mapper.dataobject.BlogDO;
import com.coderman.domainservice.blogtopic.infrast.mapper.dataobject.BlogSpecialColumnRelationDO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * date: 2021/3/29
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class BlogRepositoryImpl implements BlogRepository {
    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private BlogContentMapper blogContentMapper;

    @Autowired
    private BlogCommentMapper blogCommentMapper;

    @Autowired
    private BlogSpecialColumnRelationMapper blogSpecialColumnRelationMapper;

    @Override
    public void publishBlog(BlogBO blogBO) {
        BlogContentDO blogContentDO = new BlogContentDO();
        BlogDO blogDO = BlogConvertor.INSTANCE.bo2do(blogBO);
        long id = blogMapper.insert(blogDO);
        blogContentDO.setBlogId(id);
        blogContentDO.setBlogContent(blogBO.getBlogContent());
        blogContentMapper.insert(blogContentDO);
        List<BlogSpecialColumnRelationDO> blogSpecialColumnRelationDOList = new ArrayList<>();
        blogBO.getBlogSpecialIdList().stream().forEach(columnId->{
            BlogSpecialColumnRelationDO  blogSpecialColumnRelationDO = new BlogSpecialColumnRelationDO();
            blogSpecialColumnRelationDO.setBlogId(id);
            blogSpecialColumnRelationDO.setBlogSpecialColumnId(columnId);
            blogSpecialColumnRelationDOList.add(blogSpecialColumnRelationDO);
        });
        blogSpecialColumnRelationMapper.insertBatch(blogSpecialColumnRelationDOList);

    }

    @Override
    public void dealBlog(Long blogId, int status) {

    }

    @Override
    public void publishComment(BlogCommentBO blogCommentBO) {
        //存储评论
    }

    @Override
    public List<BlogAggregateBO> findBlog(Long userId) {
        return null;
    }

    @Override
    public BlogAggregateBO findOneBlog(long blogId) {
        BlogDO blogDO = blogMapper.findById(blogId);
        BlogAggregateBO blogAggregateBO = new BlogAggregateBO();
        blogAggregateBO.setBlogBO(BlogConvertor.INSTANCE.do2bo(blogDO));
        List<BlogCommentDO> blogCommentDOList = blogCommentMapper.findByBlogId(blogId);
        blogAggregateBO.setBlogCommentBOList(BlogCommentConvertor.INSTANCE.dolist2bolist(blogCommentDOList));
        // blogAggregateBO.setBlogSpecialColumnBOList();
        return blogAggregateBO;
    }
}
